package com.mission.schedule.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.DownLoadBackBean;
import com.mission.schedule.bean.DownLoadBean;
import com.mission.schedule.bean.DownLoadRepeatBackBean;
import com.mission.schedule.bean.DownLoadRepeatBean;
import com.mission.schedule.bean.RepeatUpAndDownBean;
import com.mission.schedule.bean.ScheduBean;
import com.mission.schedule.bean.SetBackBean;
import com.mission.schedule.bean.SetBean;
import com.mission.schedule.bean.qd606.NoteTitleDetailBean;
import com.mission.schedule.clock.QueryAlarmData;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.constants.URLConstants;
import com.mission.schedule.utils.CalendarChangeValue;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.SharedPrefUtil;
import com.mission.schedule.utils.XmlUtil;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 网络数据下载操作，下载个人日程 日程分类，重复，本地联系人，清单标题，清单详情，清单分类，个人信息设置
 * Created by liwei on 2017/5/18.
 */

public class ServerDataDownLocalServicecopy extends Service {
    String path = "";
    App application = App.getDBcApplication();
    SharedPrefUtil sharedPrefUtil = null;
    int index;
    List<Map<String, String>> soundlist = new ArrayList<Map<String, String>>();
    String UserId = "";
    String schyear;
    String schtime;
    String repyear;
    String reptime;
    Intent ServerDataDownLocalReceiver;
    String downreppath;
    CalendarChangeValue changeValue = new CalendarChangeValue();
    String downtime = "2016-01-01";
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        soundlist.clear();
        soundlist = XmlUtil.readBeforeBellXML(this);
        sharedPrefUtil = new SharedPrefUtil(getApplication(),
                ShareFile.USERFILE);
        UserId = sharedPrefUtil.getString(getApplicationContext(),
                ShareFile.USERFILE, ShareFile.USERID, "");
        downtime = sharedPrefUtil.getString(
                getApplication(), ShareFile.USERFILE,
                ShareFile.UPDATENOTESETTIME, "2016-01-01");
        String date = DateUtil.formatDateTimeSs(new Date());
        int year1 = Integer.parseInt(date.substring(0, 4)
                .toString()) - 1;
        
        if ("0".equals(sharedPrefUtil.getString(getApplication(),
                ShareFile.USERFILE, ShareFile.FIRSTLOGIN, "0"))) {
            schyear = String.valueOf(year1)
                    + date.substring(4, 10).toString();
            schtime = date.substring(11, 19).toString();
            repyear = String.valueOf(year1)
                    + date.substring(4, 10).toString();
            reptime = date.substring(11, 19).toString();
        } else {
            String schdate = sharedPrefUtil.getString(
                    getApplication(), ShareFile.USERFILE,
                    ShareFile.DOWNSCHTIME, "");
            if (!"".equals(schdate)) {
                try {
                    schyear = schdate.substring(0, 10);
                    schtime = schdate.substring(11);
                } catch (Exception e) {
                    schyear = DateUtil.formatDate(new Date());
                    schtime = DateUtil.formatDateTimeSs(new Date()).substring(11);
                }
            } else {
                schyear = String.valueOf(year1)
                        + date.substring(4, 10).toString();
                schtime = date.substring(11, 19).toString();
            }
            String repdate = sharedPrefUtil.getString(
                    getApplication(), ShareFile.USERFILE,
                    ShareFile.DOWNREPTIME, "");
            if (!"".equals(repdate)) {
                try {
                    repyear = repdate.substring(0, 10);
                    reptime = repdate.substring(11);
                } catch (Exception e) {
                    repyear = DateUtil.formatDate(new Date());
                    reptime = DateUtil.formatDateTimeSs(new Date()).substring(11);
                }
            } else {
                repyear = String.valueOf(year1)
                        + date.substring(4, 10).toString();
                reptime = date.substring(11, 19).toString();
            }
        }
        downCalender();
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 下载日程数据
     */
    private void downCalender(){

        if (sharedPrefUtil.getString(getApplicationContext(),
                ShareFile.USERFILE, ShareFile.LOGINTYPE, "1").equals("1")) {
            path = URLConstants.数据下载同步 + "?cUid=" + UserId + "&beforDownTime=" + schyear
                    + "&time=" + schtime + "&type=1";
            sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.LOGINTYPE, "0");
        } else {
            path = URLConstants.数据下载同步 + "?cUid=" + UserId + "&beforDownTime=" + schyear
                    + "&time=" + schtime;
        }
        if (sharedPrefUtil.getString(getApplication(), ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
            StringRequest request = new StringRequest(Method.GET, path,
                    new Response.Listener<String>() {

                        @Override
                        public void onResponse(final String result) {
                            if (!TextUtils.isEmpty(result)) {
                                new Thread(new Runnable() {

                                    @Override
                                    public void run() {
                                        try {
                                            Gson gson = new Gson();
                                            DownLoadBackBean downLoadBean = gson.fromJson(result, DownLoadBackBean.class);

                                            List<ScheduBean> list = null;
                                            List<Integer> deleteID = null;
                                            if (downLoadBean.status == 0) {
                                                sharedPrefUtil.putString(
                                                        getApplication(),
                                                        ShareFile.USERFILE,
                                                        ShareFile.DOWNSCHTIME,
                                                        downLoadBean.message.replace("T", " "));
                                                List<DownLoadBean> beans = downLoadBean.maps;
                                                for (int k = 0; k < beans.size(); k++) {
                                                    deleteID = beans.get(k).delete;
                                                    list = beans.get(k).tbCalendar;
                                                }
                                                if (list != null && list.size() > 0) {
                                                    for (int i = 0; i < list.size(); i++) {
                                                        String ringcode = "";
                                                        String ringdesc = "";
                                                        for (int j = 0; j < soundlist.size(); j++) {
                                                            String str = list.get(i).cAlarmSoundDesc.replace(".ogg", "");
                                                            if (str.equals(soundlist.get(j).get("value"))) {
                                                                ringcode = soundlist.get(j).get("value");
                                                                ringdesc = soundlist.get(j).get("name");
                                                                break;
                                                            } else {
                                                                ringcode = "g_88";
                                                                ringdesc = "完成任务";
                                                            }
                                                        }
                                                        String str = list.get(i).cUpdateTime.toString().replace("T", " ");
                                                        int m = application.CheckCountSchData(list.get(i).cId);
                                                        int aid = list.get(i).attentionid;
                                                        int atype = list.get(i).aType;
                                                        if (m == 0) {
                                                            application.insertIntenetScheduleData(
                                                                            list.get(i).cId,
                                                                            list.get(i).cContent,
                                                                            list.get(i).cDate,
                                                                            list.get(i).cTime,
                                                                            list.get(i).cIsAlarm,
                                                                            list.get(i).cBeforTime,
                                                                            list.get(i).cDisplayAlarm,
                                                                            list.get(i).cPostpone,
                                                                            list.get(i).cImportant,
                                                                            list.get(i).cColorType,
                                                                            list.get(i).cIsEnd,
                                                                            str,
                                                                            list.get(i).cTags,
                                                                            list.get(i).cType,
                                                                            list.get(i).cTypeDesc,
                                                                            list.get(i).cTypeSpare,
                                                                            list.get(i).cRepeatId,
                                                                            list.get(i).cRepeatDate,
                                                                            str,
                                                                            0,
                                                                            list.get(i).cOpenState,
                                                                            list.get(i).cSchRepeatLink,
                                                                            ringdesc,
                                                                            ringcode,
                                                                            list.get(i).cRecommendName,
                                                                            list.get(i).schRead,
                                                                            aid,
                                                                            atype,
                                                                            list.get(i).webUrl,
                                                                            list.get(i).imgPath,
                                                                            1,
                                                                            0,
                                                                            list.get(i).cRecommendId,list.get(i).pIsEnd, list.get(i).remark,
                                                                    list.get(i).remark1,list.get(i).remark2,list.get(i).remark3,list.get(i).remark4
                                                                    ,list.get(i).remark7,list.get(i).remark10,list.get(i).remark11,list.get(i).remark12);
                                                        } else {
                                                            application.updateScheduleData(
                                                                            list.get(i).cId,
                                                                            list.get(i).cContent,
                                                                            list.get(i).cDate,
                                                                            list.get(i).cTime,
                                                                            list.get(i).cIsAlarm,
                                                                            list.get(i).cBeforTime,
                                                                            list.get(i).cDisplayAlarm,
                                                                            list.get(i).cPostpone,
                                                                            list.get(i).cImportant,
                                                                            list.get(i).cColorType,
                                                                            list.get(i).cIsEnd,
                                                                            list.get(i).cTags,
                                                                            list.get(i).cType,
                                                                            list.get(i).cTypeDesc,
                                                                            list.get(i).cTypeSpare,
                                                                            list.get(i).cRepeatId,
                                                                            list.get(i).cRepeatDate,
                                                                            str,
                                                                            0,
                                                                            list.get(i).cOpenState,
                                                                            list.get(i).cSchRepeatLink,
                                                                            ringdesc,
                                                                            ringcode,
                                                                            list.get(i).cRecommendName,
                                                                            list.get(i).schRead,
                                                                            aid,
                                                                            list.get(i).aType,
                                                                            list.get(i).webUrl,
                                                                            list.get(i).imgPath,
                                                                            0,
                                                                            0,
                                                                            list.get(i).cRecommendId,list.get(i).pIsEnd, list.get(i).remark,
                                                                    list.get(i).remark1,
                                                                    list.get(i).remark2,
                                                                    list.get(i).remark3,
                                                                    list.get(i).remark4,list.get(i).remark7,list.get(i).remark10,list.get(i).remark11,list.get(i).remark12);
                                                        }
                                                    }
                                                }

                                                if (deleteID != null && deleteID.size() > 0) {
                                                    for (int i = 0; i < deleteID.size(); i++) {
                                                        application.deleteScheduleData(deleteID.get(i));
                                                        application.deleteSch(deleteID.get(i));
                                                    }
                                                }
                                                QueryAlarmData.writeAlarm(getApplicationContext());
                                                sharedPrefUtil.putString(getApplication(), ShareFile.USERFILE,
                                                                ShareFile.INDEX, index + "");
                                                downCalenderStatus(false);
                                            } else if (downLoadBean.status == 1) {
                                                sharedPrefUtil.putString(getApplication(),
                                                        ShareFile.USERFILE, ShareFile.DOWNSCHTIME,
                                                        downLoadBean.message.replace("T", " "));
                                                sharedPrefUtil.putString(getApplication(), ShareFile.USERFILE,
                                                                ShareFile.INDEX, index + "");
                                                downCalenderStatus(true);
                                            } else {
                                                sharedPrefUtil.putString(getApplication(), ShareFile.USERFILE,
                                                                ShareFile.INDEX, index + "");
                                                downCalenderStatus(true);
                                            }
                                        } catch (JsonSyntaxException e) {
                                            e.printStackTrace();
                                            sharedPrefUtil.putString(getApplication(),
                                                    ShareFile.USERFILE, ShareFile.INDEX, index + "");
                                            downCalenderStatus(true);
                                        }
                                    }
                                }).start();

                            }else {
                                downCalenderStatus(true);
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    downCalenderStatus(true);
                }
            });
            request.setTag("downCalender");
            request.setRetryPolicy(new DefaultRetryPolicy(10000, 1, 1.0f));
            App.getHttpQueues().add(request);
        }
    }

    private void downCalenderStatus(boolean isFailed){
        ServerDataDownLocalReceiver = new Intent("ServerDataDownLocalReceiver");
        ServerDataDownLocalReceiver.putExtra("downCalender", isFailed ? "downCalender_false" : "downCalender");
        sendBroadcast(ServerDataDownLocalReceiver);
        downRepeat();
    }
    /**
     * 下载重复数据
     */
    private void downRepeat(){
        if (sharedPrefUtil.getString(getApplicationContext(),
                ShareFile.USERFILE, ShareFile.LOGINTYPE, "1").equals("1")) {
            downreppath = URLConstants.重复数据下载 + "?cUid="
                    + UserId + "&beforDownTime=" + repyear
                    + "&time=" + reptime + "&type=1";
            sharedPrefUtil.putString(getApplicationContext(),
                    ShareFile.USERFILE, ShareFile.LOGINTYPE, "0");
        } else {
            downreppath = URLConstants.重复数据下载 + "?cUid="
                    + UserId + "&beforDownTime=" + repyear
                    + "&time=" + reptime;
        }
        if (sharedPrefUtil.getString(getApplication(),
                ShareFile.USERFILE, ShareFile.ISNEWYOUKE, "0").equals("-1")) {
            StringRequest request = new StringRequest(Method.GET, downreppath,
                    new Response.Listener<String>() {

                        @Override
                        public void onResponse(final String result) {
                            if (!TextUtils.isEmpty(result)) {
                                new Thread(new Runnable() {

                                    @Override
                                    public void run() {
                                        try {
                                            Gson gson = new Gson();
                                            DownLoadRepeatBackBean downLoadBean = null;


                                            downLoadBean = gson.fromJson(result,
                                                    DownLoadRepeatBackBean.class);

                                            List<DownLoadRepeatBean> beans;
                                            List<RepeatUpAndDownBean> list = null;
                                            List<Integer> deleteID = null;
                                            if (downLoadBean.status == 0) {
                                                sharedPrefUtil.putString(
                                                        getApplication(),
                                                        ShareFile.USERFILE,
                                                        ShareFile.DOWNREPTIME,
                                                        downLoadBean.message
                                                                .replace("T", " "));
                                                beans = downLoadBean.maps;
                                                for (int k = 0; k < beans.size(); k++) {
                                                    deleteID = beans.get(k).delete;
                                                    list = beans.get(k).TbTtimepreinstall;
                                                }

                                                if (list != null && list.size() > 0) {
                                                    for (int i = 0; i < list.size(); i++) {
                                                        String ringcode = "";
                                                        String ringdesc = "";
                                                        for (int j = 0; j < soundlist
                                                                .size(); j++) {
                                                            String str = list
                                                                    .get(i).repRingCode
                                                                    .replace(
                                                                            ".ogg",
                                                                            "");
                                                            if (str.equals(soundlist
                                                                    .get(j)
                                                                    .get("value"))) {
                                                                ringcode = soundlist
                                                                        .get(j)
                                                                        .get("value");
                                                                ringdesc = soundlist
                                                                        .get(j)
                                                                        .get("name");
                                                                break;
                                                            } else {
                                                                ringcode = "g_88";
                                                                ringdesc = "完成任务";
                                                            }
                                                        }
                                                        int index = 0;
                                                        if (list.get(i).recommendedUserId != null
                                                                && !"".equals(list
                                                                .get(i).recommendedUserId)) {
                                                            index = Integer.parseInt(list
                                                                    .get(i).recommendedUserId);
                                                        }
                                                        // 根据repid查询重复表，看下是否有repid相同的母记事
                                                        int count = application.CheckCountRepData(Integer.parseInt(list.get(i).repId));
                                                        if (count == 0) {
                                                            if (("".equals(list.get(i).repTypeParameter) || "null".equals(list.get(i).repTypeParameter))
                                                                    && (!"1".equals(list.get(i).repType) || !"5".equals(list.get(i).repType))) {
                                                                String typestr;
                                                                int type;
                                                                String parstr = "";
                                                                if ("1".equals(list.get(i).repType)) {
                                                                    typestr = "[" + "" + "]";
                                                                    type = 1;
                                                                } else if ("2".equals(list.get(i).repType)) {
                                                                    typestr = "[" + "1" + "]";
                                                                    type = 2;
                                                                } else if ("3".equals(list.get(i).repType)) {
                                                                    typestr = "[" + "\"" + 1 + "\"" + "]";
                                                                    type = 3;
                                                                } else if ("4".equals(list.get(i).repType)) {
                                                                    typestr = "[" + "\"" + "01-01" + "\"" + "]";
                                                                    type = 4;
                                                                } else if ("6".equals(list.get(i).repType)) {
                                                                    typestr = "[" + "\"" + "正月初一" + "\"" + "]";
                                                                    type = 6;
                                                                    parstr = "01-01";
                                                                } else {
                                                                    typestr = "[" + "" + "]";
                                                                    type = 5;
                                                                }
                                                                application.insertDownCLRepeatTableData(
                                                                        Integer.parseInt(list.get(i).repId),
                                                                        Integer.parseInt(list.get(i).repBeforeTime),
                                                                        Integer.parseInt(list.get(i).repColorType),
                                                                        Integer.parseInt(list.get(i).repDisplayTime),
                                                                        type,
                                                                        Integer.parseInt(list.get(i).repIsAlarm),
                                                                        Integer.parseInt(list.get(i).repIsPuase),
                                                                        Integer.parseInt(list.get(i).repIsImportant),
                                                                        Integer.parseInt(list.get(i).repSourceType),
                                                                        0,
                                                                        typestr,
                                                                        list.get(i).repNextCreatedTime.replace("T", " "),
                                                                        list.get(i).repLastCreatedTime.replace("T", " "),
                                                                        list.get(i).repInitialCreatedTime.replace("T", " "),
                                                                        list.get(i).repStartDate.replace("T", " "),
                                                                        list.get(i).repContent,
                                                                        list.get(i).repCreateTime.replace("T", " "),
                                                                        list.get(i).repSourceDesc,
                                                                        list.get(i).repSourceDescSpare,
                                                                        list.get(i).repTime,
                                                                        ringdesc,
                                                                        ringcode,
                                                                        list.get(i).repUpdateTime.replace("T", " "),
                                                                        Integer.parseInt(list.get(i).repOpenState),
                                                                        list.get(i).recommendedUserName,
                                                                        index,
                                                                        list.get(i).repdateone,
                                                                        list.get(i).repdatetwo,
                                                                        Integer.parseInt(list.get(i).repstateone),
                                                                        Integer.parseInt(list.get(i).repstatetwo),
                                                                        list.get(i).aType,
                                                                        list.get(i).webUrl,
                                                                        list.get(i).imgPath,
                                                                        list.get(i).repInSTable,
                                                                        list.get(i).repEndState,
                                                                        parstr,
                                                                        list.get(i).repRead, 0
                                                                        ,list.get(i).dataType,list.get(i).dataId,list.get(i).remark1,
                                                                        list.get(i).remark2,list.get(i).remark3,list.get(i).remark4,list.get(i).remark5);
                                                            } else {
                                                                /**
                                                                 * 重复 每年
                                                                 */
                                                                String parstr = "";
                                                                if ("6".equals(list.get(i).repType)) {
                                                                    if (changeValue.changaSZ(list.get(i).repTypeParameter
                                                                            .replace("[", "")
                                                                            .replace("]", "")
                                                                            .replace("\n\"", "")
                                                                            .replace("\n", "")
                                                                            .replace("\"", ""))
                                                                            .length() == 2) {
                                                                        parstr = DateUtil.formatDate(new Date()).substring(5, 7)
                                                                                + "-"
                                                                                + changeValue.changaSZ(list.get(i).repTypeParameter
                                                                                .replace("[", "")
                                                                                .replace("]", "")
                                                                                .replace("\n\"", "")
                                                                                .replace("\n", "")
                                                                                .replace("\"", ""));
                                                                    } else {
                                                                        parstr = changeValue.changaSZ(list.get(i).repTypeParameter
                                                                                .replace("[", "")
                                                                                .replace("]", "")
                                                                                .replace("\n\"", "")
                                                                                .replace("\n", "")
                                                                                .replace("\"", ""));
                                                                    }
                                                                }
                                                                /**
                                                                 * 这里应该有判断
                                                                 */
                                                                application.insertDownCLRepeatTableData(
                                                                        Integer.parseInt(list.get(i).repId),
                                                                        Integer.parseInt(list.get(i).repBeforeTime),
                                                                        Integer.parseInt(list.get(i).repColorType),
                                                                        Integer.parseInt(list.get(i).repDisplayTime),
                                                                        Integer.parseInt(list.get(i).repType),
                                                                        Integer.parseInt(list.get(i).repIsAlarm),
                                                                        Integer.parseInt(list.get(i).repIsPuase),
                                                                        Integer.parseInt(list.get(i).repIsImportant),
                                                                        Integer.parseInt(list.get(i).repSourceType),
                                                                        0,
                                                                        list.get(i).repTypeParameter,
                                                                        list.get(i).repNextCreatedTime.replace("T", " "),
                                                                        list.get(i).repLastCreatedTime.replace("T", " "),
                                                                        list.get(i).repInitialCreatedTime.replace("T", " "),
                                                                        list.get(i).repStartDate.replace("T", " "),
                                                                        list.get(i).repContent,
                                                                        list.get(i).repCreateTime.replace("T", " "),
                                                                        list.get(i).repSourceDesc,
                                                                        list.get(i).repSourceDescSpare,
                                                                        list.get(i).repTime,
                                                                        ringdesc,
                                                                        ringcode,
                                                                        list.get(i).repUpdateTime.replace("T", " "),
                                                                        Integer.parseInt(list.get(i).repOpenState),
                                                                        list.get(i).recommendedUserName,
                                                                        index,
                                                                        list.get(i).repdateone,
                                                                        list.get(i).repdatetwo,
                                                                        Integer.parseInt(list.get(i).repstateone),
                                                                        Integer.parseInt(list.get(i).repstatetwo),
                                                                        list.get(i).aType,
                                                                        list.get(i).webUrl,
                                                                        list.get(i).imgPath,
                                                                        list.get(i).repInSTable,
                                                                        list.get(i).repEndState,
                                                                        parstr,
                                                                        list.get(i).repRead, 0
                                                                        ,list.get(i).dataType,list.get(i).dataId,list.get(i).remark1,
                                                                        list.get(i).remark2,list.get(i).remark3,list.get(i).remark4,list.get(i).remark5);
                                                            }
                                                            //是否生成子记事标记 0 生成
                                                            if (list.get(i).repInSTable == 0) {
                                                                //未暂停才生出
                                                                if ("0".equals(list.get(i).repIsPuase)) {
//                                                                    if (Integer.parseInt(list.get(i).repstatetwo) == 0) {
                                                                        DownRepeatService.CreateRepeatSchNextData(list.get(i));
//                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                                if (deleteID != null && deleteID.size() > 0) {
                                                    for (int i = 0; i < deleteID.size(); i++) {
                                                        application.deleteChildSch(deleteID.get(i) + "");
                                                        application.deleteRepeatData(deleteID.get(i).toString());
                                                        application.deleteSch(deleteID.get(i));
                                                    }
                                                }
                                                if ((list != null && list.size() > 0) || (deleteID != null && deleteID.size() > 0)) {
                                                    Intent intent = new Intent();
                                                    intent.setAction(DownRepeatService.RepUPDATADATA);
                                                    intent.putExtra("data", "success");
                                                    sendBroadcast(intent);
                                                }
                                                downRepeatStatus(false);
                                            } else if (downLoadBean.status == 1) {
                                                sharedPrefUtil.putString(
                                                        getApplication(),
                                                        ShareFile.USERFILE,
                                                        ShareFile.DOWNREPTIME,
                                                        downLoadBean.message.replace("T", " "));
                                                downRepeatStatus(true);
                                            }
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                            downRepeatStatus(true);

                                        }
                                    }
                                }).start();

                            }else {
                                downRepeatStatus(true);
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    downRepeatStatus(true);
                }
            });
            request.setTag("downRepeat");
            request.setRetryPolicy(new DefaultRetryPolicy(10000, 1, 1.0f));
            App.getHttpQueues().add(request);
        }

    }

    private void downRepeatStatus(boolean isFailed){
        ServerDataDownLocalReceiver = new Intent("ServerDataDownLocalReceiver");
        ServerDataDownLocalReceiver.putExtra("downRepeat", isFailed ? "downRepeat_false" : "downRepeat");
        sendBroadcast(ServerDataDownLocalReceiver);
        downNote();
    }
    /**
     * 下载清单数据
     */
    private void downNote(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                StringRequest stringRequest = new StringRequest(Method.POST, URLConstants.清单和清单标题下行,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String result) {
                                if (!TextUtils.isEmpty(result)) {
                                    try {
                                        Gson gson = new Gson();
                                        NoteTitleDetailBean bean = gson.fromJson(result, NoteTitleDetailBean.class);
                                        if (bean.status == 0) {
                                            sharedPrefUtil
                                                    .putString(
                                                            getApplication(),
                                                            ShareFile.USERFILE,
                                                            ShareFile.UPDATENOTESETTIME,
                                                            bean.downTime.replace('T', ' '));
                                            List<NoteTitleDetailBean.TDelListBean> tDelList = bean.tDelList;
                                            List<NoteTitleDetailBean.ListBean> beanList = bean.list;
                                            List<NoteTitleDetailBean.DelListBean> delList = bean.delList;

                                            if (tDelList != null) {
                                                boolean isTitleBWL = false;
                                                for (NoteTitleDetailBean.TDelListBean tDelListBean : tDelList) {
                                                    if (tDelListBean.titles.equals("备忘录")){
                                                        if (!isTitleBWL){
                                                            App.getDBcApplication().saveNoteTitleData(tDelListBean);
                                                        }
                                                        isTitleBWL = true;
                                                    }else {
                                                        App.getDBcApplication().saveNoteTitleData(tDelListBean);
                                                    }
                                                }
                                            }

                                            if (beanList != null) {
                                                for (NoteTitleDetailBean.ListBean listBean : beanList) {
                                                    if (!listBean.contents.isEmpty()) {
                                                        App.getDBcApplication().saveNoteDetailData(listBean);
                                                    }
                                                }
                                            }

                                            if (delList != null) {
                                                for (NoteTitleDetailBean.DelListBean delListBean : delList) {

                                                    App.getDBcApplication().deleteNote(delListBean.type, delListBean.dataId);
                                                }
                                            }
                                            downNoteStatus(true);
                                        }else {downNoteStatus(true);}
                                    } catch (JsonSyntaxException e) {
                                        e.printStackTrace();
                                        downNoteStatus(true);
                                    }
                                }else {
                                    downNoteStatus(true);
                                }
                            }

                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.d("TAG", "error:" + volleyError.toString());
                        downNoteStatus(true);
                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String, String> map = new HashMap<>();
                        map.put("uId", UserId);// "52432"
                        map.put("changeTime", downtime.replace('T', ' '));//downtime.replace('T', ' ')
                        return map;
                    }
                };
                stringRequest.setTag("downnote");
                stringRequest.setRetryPolicy(new DefaultRetryPolicy(10000, 1, 1.0f));
                App.queues.add(stringRequest);
            }
        }).start();
    }

    private void downNoteStatus(boolean isFailed){
        ServerDataDownLocalReceiver = new Intent("ServerDataDownLocalReceiver");
        ServerDataDownLocalReceiver.putExtra("downNote", isFailed ? "downNote_false" : "downNote");
        sendBroadcast(ServerDataDownLocalReceiver);
        downPerson();
    }
    /**
     * 下载个人信息
     */
    private void downPerson(){
        final String updatetime = sharedPrefUtil.getString(getApplicationContext(),
                ShareFile.USERFILE, ShareFile.UPDATESETTIME, "");
        String querysetPath = URLConstants.查询设置信息 + UserId;
        StringRequest request = new StringRequest(Method.GET,
                querysetPath, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if (!TextUtils.isEmpty(s)) {
                    try {
                        Gson gson = new Gson();
                        SetBackBean backBean = gson.fromJson(s,
                                SetBackBean.class);
                        if (backBean.status == 0) {
                            List<SetBean> list = backBean.list;
                            if ("".equals(updatetime)) {
                                sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.MUSICDESC, list.get(0).ringDesc + "");
                                sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.MUSICCODE, list.get(0).ringCode + "");
                                sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.BEFORETIME, list.get(0).beforeTime + "");
                                sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.MORNINGSTATE, list.get(0).morningState + "");
                                sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.MORNINGTIME, list.get(0).morningTime + "");
                                sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.NIGHTSTATE, list.get(0).nightState + "");
                                sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.NIGHTTIME, list.get(0).nightTime + "");
                                sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.ALLSTATE, list.get(0).dayState + "");
                                sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.ALLTIME, list.get(0).dayTime + "");
                                sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.UPDATESETTIME, list.get(0).updateTime.replace("T", " "));
                                sharedPrefUtil.putString(getApplicationContext(), ShareFile.USERFILE, ShareFile.SETID, list.get(0).id + "");
                                String today, tomorrow;
                                Calendar calendar = Calendar.getInstance();
                                calendar.setTime(new Date());
                                today = DateUtil.formatDate(calendar.getTime());
                                calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
                                tomorrow = DateUtil.formatDate(calendar.getTime());
                                if ("0".equals(sharedPrefUtil.getString(getApplicationContext(), ShareFile.USERFILE, ShareFile.MORNINGSTATE, "0"))) {
                                    int count = App.getDBcApplication().CheckClockIDData(-1);
                                    if (count == 0) {
                                        if (DateUtil.parseDateTimeHm(sharedPrefUtil.getString(getApplicationContext(), ShareFile.USERFILE, ShareFile.MORNINGTIME, "07:58"))
                                                .before(DateUtil.parseDateTimeHm(DateUtil.formatDateTimeHm(new Date())))) {
                                            App.getDBcApplication()
                                                    .insertEveryClockData(-1, DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(tomorrow + " " + sharedPrefUtil.getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.MORNINGTIME,
                                                                                    "07:58"))),
                                                            "早上问候",
                                                            0,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(tomorrow
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.MORNINGTIME,
                                                                                    "07:58"))),
                                                            "",
                                                            "morninghello.wav",
                                                            1, 1,
                                                            7, 0,
                                                            0, 1,
                                                            0, "",
                                                            0, 0,
                                                            "", "");
                                        } else {
                                            App.getDBcApplication()
                                                    .insertEveryClockData(
                                                            -1,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(today
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.MORNINGTIME,
                                                                                    "07:58"))),
                                                            "早上问候",
                                                            0,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(today
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.MORNINGTIME,
                                                                                    "07:58"))),
                                                            "",
                                                            "morninghello.wav",
                                                            1, 1,
                                                            7, 0,
                                                            0, 1,
                                                            0, "",
                                                            0, 0,
                                                            "", "");
                                        }
                                    } else {
                                        App.getDBcApplication()
                                                .deleteEveryClock(
                                                        -1);
                                        if (DateUtil.parseDateTimeHm(sharedPrefUtil.getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.MORNINGTIME,
                                                                        "07:58")).before(DateUtil.parseDateTimeHm(DateUtil.formatDateTimeHm(new Date())))) {
                                            App.getDBcApplication()
                                                    .insertEveryClockData(
                                                            -1,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(tomorrow
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.MORNINGTIME,
                                                                                    "07:58"))),
                                                            "早上问候",
                                                            0,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(tomorrow
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.MORNINGTIME,
                                                                                    "07:58"))),
                                                            "",
                                                            "morninghello.wav",
                                                            1, 1,
                                                            7, 0,
                                                            0, 1,
                                                            0, "",
                                                            0, 0,
                                                            "", "");
                                        } else {
                                            App.getDBcApplication()
                                                    .insertEveryClockData(
                                                            -1,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(today
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.MORNINGTIME,
                                                                                    "07:58"))),
                                                            "早上问候",
                                                            0,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(today
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.MORNINGTIME,
                                                                                    "07:58"))),
                                                            "",
                                                            "morninghello.wav",
                                                            1, 1,
                                                            7, 0,
                                                            0, 1,
                                                            0, "",
                                                            0, 0,
                                                            "", "");
                                        }
                                    }
                                } else {
                                    App.getDBcApplication()
                                            .deleteEveryClock(-1);
                                }
                                if ("0".equals(sharedPrefUtil
                                        .getString(
                                                getApplicationContext(),
                                                ShareFile.USERFILE,
                                                ShareFile.NIGHTSTATE,
                                                "0"))) {

                                    int count = App
                                            .getDBcApplication()
                                            .CheckClockIDData(-2);
                                    if (count == 0) {
                                        if (DateUtil
                                                .parseDateTimeHm(
                                                        sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.NIGHTTIME,
                                                                        "20:58"))
                                                .before(DateUtil
                                                        .parseDateTimeHm(DateUtil
                                                                .formatDateTimeHm(new Date())))) {
                                            App.getDBcApplication()
                                                    .insertEveryClockData(
                                                            -2,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(tomorrow
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.NIGHTTIME,
                                                                                    "20:58"))),
                                                            "下午问候",
                                                            0,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(tomorrow
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.NIGHTTIME,
                                                                                    "20:58"))),
                                                            "",
                                                            "nighthello.ogg",
                                                            1, 1,
                                                            7, 0,
                                                            0, 1,
                                                            0, "",
                                                            0, 0,
                                                            "", "");
                                        } else {
                                            App.getDBcApplication()
                                                    .insertEveryClockData(
                                                            -2,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(today
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.NIGHTTIME,
                                                                                    "20:58"))),
                                                            "下午问候",
                                                            0,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(today
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.NIGHTTIME,
                                                                                    "20:58"))),
                                                            "",
                                                            "nighthello.ogg",
                                                            1, 1,
                                                            7, 0,
                                                            0, 1,
                                                            0, "",
                                                            0, 0,
                                                            "", "");
                                        }
                                    } else {
                                        App.getDBcApplication()
                                                .deleteEveryClock(
                                                        -2);
                                        if (DateUtil
                                                .parseDateTimeHm(
                                                        sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.NIGHTTIME,
                                                                        "20:58"))
                                                .before(DateUtil
                                                        .parseDateTimeHm(DateUtil
                                                                .formatDateTimeHm(new Date())))) {
                                            App.getDBcApplication()
                                                    .insertEveryClockData(
                                                            -2,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(tomorrow
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.NIGHTTIME,
                                                                                    "20:58"))),
                                                            "下午问候",
                                                            0,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(tomorrow
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.NIGHTTIME,
                                                                                    "20:58"))),
                                                            "",
                                                            "nighthello.ogg",
                                                            1, 1,
                                                            7, 0,
                                                            0, 1,
                                                            0, "",
                                                            0, 0,
                                                            "", "");
                                        } else {
                                            App.getDBcApplication()
                                                    .insertEveryClockData(
                                                            -2,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(today
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.NIGHTTIME,
                                                                                    "20:58"))),
                                                            "下午问候",
                                                            0,
                                                            DateUtil.formatDateTimeSs(DateUtil
                                                                    .parseDateTime(today
                                                                            + " "
                                                                            + sharedPrefUtil
                                                                            .getString(
                                                                                    getApplicationContext(),
                                                                                    ShareFile.USERFILE,
                                                                                    ShareFile.NIGHTTIME,
                                                                                    "20:58"))),
                                                            "",
                                                            "nighthello.ogg",
                                                            1, 1,
                                                            7, 0,
                                                            0, 1,
                                                            0, "",
                                                            0, 0,
                                                            "", "");
                                        }
                                    }
                                } else {
                                    App.getDBcApplication()
                                            .deleteEveryClock(-2);
                                }
                            } else {
                                if (DateUtil
                                        .parseDateTimeSs(
                                                list.get(0).updateTime
                                                        .replace(
                                                                "T",
                                                                " "))
                                        .after(DateUtil.parseDateTimeSs(updatetime))) {
                                    sharedPrefUtil.putString(
                                            getApplicationContext(),
                                            ShareFile.USERFILE,
                                            ShareFile.MUSICDESC,
                                            list.get(0).ringDesc
                                                    + "");
                                    sharedPrefUtil.putString(
                                            getApplicationContext(),
                                            ShareFile.USERFILE,
                                            ShareFile.MUSICCODE,
                                            list.get(0).ringCode
                                                    + "");
                                    sharedPrefUtil.putString(
                                            getApplicationContext(),
                                            ShareFile.USERFILE,
                                            ShareFile.BEFORETIME,
                                            list.get(0).beforeTime
                                                    + "");
                                    sharedPrefUtil
                                            .putString(
                                                    getApplicationContext(),
                                                    ShareFile.USERFILE,
                                                    ShareFile.MORNINGSTATE,
                                                    list.get(0).morningState
                                                            + "");
                                    sharedPrefUtil.putString(
                                            getApplicationContext(),
                                            ShareFile.USERFILE,
                                            ShareFile.MORNINGTIME,
                                            list.get(0).morningTime
                                                    + "");
                                    sharedPrefUtil.putString(
                                            getApplicationContext(),
                                            ShareFile.USERFILE,
                                            ShareFile.NIGHTSTATE,
                                            list.get(0).nightState
                                                    + "");
                                    sharedPrefUtil.putString(
                                            getApplicationContext(),
                                            ShareFile.USERFILE,
                                            ShareFile.NIGHTTIME,
                                            list.get(0).nightTime
                                                    + "");
                                    sharedPrefUtil.putString(
                                            getApplicationContext(),
                                            ShareFile.USERFILE,
                                            ShareFile.ALLSTATE,
                                            list.get(0).dayState
                                                    + "");
                                    sharedPrefUtil.putString(
                                            getApplicationContext(),
                                            ShareFile.USERFILE,
                                            ShareFile.ALLTIME,
                                            list.get(0).dayTime
                                                    + "");
                                    sharedPrefUtil
                                            .putString(
                                                    getApplicationContext(),
                                                    ShareFile.USERFILE,
                                                    ShareFile.UPDATESETTIME,
                                                    list.get(0).updateTime
                                                            .replace(
                                                                    "T",
                                                                    " "));
                                    sharedPrefUtil.putString(
                                            getApplicationContext(),
                                            ShareFile.USERFILE,
                                            ShareFile.SETID,
                                            list.get(0).id + "");
                                    String today, tomorrow;
                                    Calendar calendar = Calendar
                                            .getInstance();
                                    calendar.setTime(new Date());
                                    today = DateUtil
                                            .formatDate(calendar
                                                    .getTime());
                                    calendar.set(
                                            Calendar.DAY_OF_MONTH,
                                            calendar.get(Calendar.DAY_OF_MONTH) + 1);
                                    tomorrow = DateUtil
                                            .formatDate(calendar
                                                    .getTime());
                                    if ("0".equals(sharedPrefUtil
                                            .getString(
                                                    getApplicationContext(),
                                                    ShareFile.USERFILE,
                                                    ShareFile.MORNINGSTATE,
                                                    "0"))) {
                                        int count = App
                                                .getDBcApplication()
                                                .CheckClockIDData(
                                                        -1);
                                        if (count == 0) {
                                            if (DateUtil
                                                    .parseDateTimeHm(
                                                            sharedPrefUtil
                                                                    .getString(
                                                                            getApplicationContext(),
                                                                            ShareFile.USERFILE,
                                                                            ShareFile.MORNINGTIME,
                                                                            "07:58"))
                                                    .before(DateUtil
                                                            .parseDateTimeHm(DateUtil
                                                                    .formatDateTimeHm(new Date())))) {
                                                App.getDBcApplication()
                                                        .insertEveryClockData(
                                                                -1,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(tomorrow
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.MORNINGTIME,
                                                                                        "07:58"))),
                                                                "早上问候",
                                                                0,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(tomorrow
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.MORNINGTIME,
                                                                                        "07:58"))),
                                                                "",
                                                                "morninghello.wav",
                                                                1,
                                                                1,
                                                                7,
                                                                0,
                                                                0,
                                                                1,
                                                                0,
                                                                "",
                                                                0,
                                                                0,
                                                                "",
                                                                "");
                                            } else {
                                                App.getDBcApplication()
                                                        .insertEveryClockData(
                                                                -1,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(today
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.MORNINGTIME,
                                                                                        "07:58"))),
                                                                "早上问候",
                                                                0,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(today
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.MORNINGTIME,
                                                                                        "07:58"))),
                                                                "",
                                                                "morninghello.wav",
                                                                1,
                                                                1,
                                                                7,
                                                                0,
                                                                0,
                                                                1,
                                                                0,
                                                                "",
                                                                0,
                                                                0,
                                                                "",
                                                                "");
                                            }
                                        } else {
                                            App.getDBcApplication()
                                                    .deleteEveryClock(
                                                            -1);
                                            if (DateUtil
                                                    .parseDateTimeHm(
                                                            sharedPrefUtil
                                                                    .getString(
                                                                            getApplicationContext(),
                                                                            ShareFile.USERFILE,
                                                                            ShareFile.MORNINGTIME,
                                                                            "07:58"))
                                                    .before(DateUtil
                                                            .parseDateTimeHm(DateUtil
                                                                    .formatDateTimeHm(new Date())))) {
                                                App.getDBcApplication()
                                                        .insertEveryClockData(
                                                                -1,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(tomorrow
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.MORNINGTIME,
                                                                                        "07:58"))),
                                                                "早上问候",
                                                                0,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(tomorrow
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.MORNINGTIME,
                                                                                        "07:58"))),
                                                                "",
                                                                "morninghello.wav",
                                                                1,
                                                                1,
                                                                7,
                                                                0,
                                                                0,
                                                                1,
                                                                0,
                                                                "",
                                                                0,
                                                                0,
                                                                "",
                                                                "");
                                            } else {
                                                App.getDBcApplication()
                                                        .insertEveryClockData(
                                                                -1,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(today
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.MORNINGTIME,
                                                                                        "07:58"))),
                                                                "早上问候",
                                                                0,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(today
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.MORNINGTIME,
                                                                                        "07:58"))),
                                                                "",
                                                                "morninghello.wav",
                                                                1,
                                                                1,
                                                                7,
                                                                0,
                                                                0,
                                                                1,
                                                                0,
                                                                "",
                                                                0,
                                                                0,
                                                                "",
                                                                "");
                                            }
                                        }
                                    } else {
                                        App.getDBcApplication()
                                                .deleteEveryClock(
                                                        -1);
                                    }
                                    if ("0".equals(sharedPrefUtil
                                            .getString(
                                                    getApplicationContext(),
                                                    ShareFile.USERFILE,
                                                    ShareFile.NIGHTSTATE,
                                                    "0"))) {

                                        int count = App
                                                .getDBcApplication()
                                                .CheckClockIDData(
                                                        -2);
                                        if (count == 0) {
                                            if (DateUtil
                                                    .parseDateTimeHm(
                                                            sharedPrefUtil
                                                                    .getString(
                                                                            getApplicationContext(),
                                                                            ShareFile.USERFILE,
                                                                            ShareFile.NIGHTTIME,
                                                                            "20:58"))
                                                    .before(DateUtil
                                                            .parseDateTimeHm(DateUtil
                                                                    .formatDateTimeHm(new Date())))) {
                                                App.getDBcApplication()
                                                        .insertEveryClockData(
                                                                -2,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(tomorrow
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.NIGHTTIME,
                                                                                        "20:58"))),
                                                                "下午问候",
                                                                0,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(tomorrow
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.NIGHTTIME,
                                                                                        "20:58"))),
                                                                "",
                                                                "nighthello.ogg",
                                                                1,
                                                                1,
                                                                7,
                                                                0,
                                                                0,
                                                                1,
                                                                0,
                                                                "",
                                                                0,
                                                                0,
                                                                "",
                                                                "");
                                            } else {
                                                App.getDBcApplication()
                                                        .insertEveryClockData(
                                                                -2,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(today
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.NIGHTTIME,
                                                                                        "20:58"))),
                                                                "下午问候",
                                                                0,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(today
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.NIGHTTIME,
                                                                                        "20:58"))),
                                                                "",
                                                                "nighthello.ogg",
                                                                1,
                                                                1,
                                                                7,
                                                                0,
                                                                0,
                                                                1,
                                                                0,
                                                                "",
                                                                0,
                                                                0,
                                                                "",
                                                                "");
                                            }
                                        } else {
                                            App.getDBcApplication()
                                                    .deleteEveryClock(
                                                            -2);
                                            if (DateUtil
                                                    .parseDateTimeHm(
                                                            sharedPrefUtil
                                                                    .getString(
                                                                            getApplicationContext(),
                                                                            ShareFile.USERFILE,
                                                                            ShareFile.NIGHTTIME,
                                                                            "20:58"))
                                                    .before(DateUtil
                                                            .parseDateTimeHm(DateUtil
                                                                    .formatDateTimeHm(new Date())))) {
                                                App.getDBcApplication()
                                                        .insertEveryClockData(
                                                                -2,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(tomorrow
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.NIGHTTIME,
                                                                                        "20:58"))),
                                                                "下午问候",
                                                                0,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(tomorrow
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.NIGHTTIME,
                                                                                        "20:58"))),
                                                                "",
                                                                "nighthello.ogg",
                                                                1,
                                                                1,
                                                                7,
                                                                0,
                                                                0,
                                                                1,
                                                                0,
                                                                "",
                                                                0,
                                                                0,
                                                                "",
                                                                "");
                                            } else {
                                                App.getDBcApplication()
                                                        .insertEveryClockData(
                                                                -2,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(today
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.NIGHTTIME,
                                                                                        "20:58"))),
                                                                "下午问候",
                                                                0,
                                                                DateUtil.formatDateTimeSs(DateUtil
                                                                        .parseDateTime(today
                                                                                + " "
                                                                                + sharedPrefUtil
                                                                                .getString(
                                                                                        getApplicationContext(),
                                                                                        ShareFile.USERFILE,
                                                                                        ShareFile.NIGHTTIME,
                                                                                        "20:58"))),
                                                                "",
                                                                "nighthello.ogg",
                                                                1,
                                                                1,
                                                                7,
                                                                0,
                                                                0,
                                                                1,
                                                                0,
                                                                "",
                                                                0,
                                                                0,
                                                                "",
                                                                "");
                                            }
                                        }
                                    } else {
                                        App.getDBcApplication()
                                                .deleteEveryClock(
                                                        -2);
                                    }
                                } else {

                                }
                            }
                        } else if (backBean.status == 1) {
                            String today, tomorrow;
                            Calendar calendar = Calendar
                                    .getInstance();
                            calendar.setTime(new Date());
                            today = DateUtil.formatDate(calendar
                                    .getTime());
                            calendar.set(
                                    Calendar.DAY_OF_MONTH,
                                    calendar.get(Calendar.DAY_OF_MONTH) + 1);
                            tomorrow = DateUtil.formatDate(calendar
                                    .getTime());
                            if ("0".equals(sharedPrefUtil
                                    .getString(getApplicationContext(),
                                            ShareFile.USERFILE,
                                            ShareFile.MORNINGSTATE,
                                            "0"))) {
                                int count = App.getDBcApplication()
                                        .CheckClockIDData(-1);
                                if (count == 0) {
                                    if (DateUtil
                                            .parseDateTimeHm(
                                                    sharedPrefUtil
                                                            .getString(
                                                                    getApplicationContext(),
                                                                    ShareFile.USERFILE,
                                                                    ShareFile.MORNINGTIME,
                                                                    "07:58"))
                                            .before(DateUtil
                                                    .parseDateTimeHm(DateUtil
                                                            .formatDateTimeHm(new Date())))) {
                                        App.getDBcApplication()
                                                .insertEveryClockData(
                                                        -1,
                                                        tomorrow
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.MORNINGTIME,
                                                                        "07:58"),
                                                        "早上问候",
                                                        0,
                                                        DateUtil.formatDateTime(new Date())
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.MORNINGTIME,
                                                                        "07:58"),
                                                        "",
                                                        "morninghello.wav",
                                                        1, 1, 7, 0,
                                                        0, 1, 0,
                                                        "", 0, 0,
                                                        "", "");
                                    } else {
                                        App.getDBcApplication()
                                                .insertEveryClockData(
                                                        -1,
                                                        today
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.MORNINGTIME,
                                                                        "07:58"),
                                                        "早上问候",
                                                        0,
                                                        today
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.MORNINGTIME,
                                                                        "07:58"),
                                                        "",
                                                        "morninghello.wav",
                                                        1, 1, 7, 0,
                                                        0, 1, 0,
                                                        "", 0, 0,
                                                        "", "");
                                    }

                                } else {
                                    App.getDBcApplication()
                                            .deleteEveryClock(-1);
                                    if (DateUtil
                                            .parseDateTimeHm(
                                                    sharedPrefUtil
                                                            .getString(
                                                                    getApplicationContext(),
                                                                    ShareFile.USERFILE,
                                                                    ShareFile.MORNINGTIME,
                                                                    "07:58"))
                                            .before(DateUtil
                                                    .parseDateTimeHm(DateUtil
                                                            .formatDateTimeHm(new Date())))) {
                                        App.getDBcApplication()
                                                .insertEveryClockData(
                                                        -1,
                                                        tomorrow
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.MORNINGTIME,
                                                                        "07:58"),
                                                        "早上问候",
                                                        0,
                                                        DateUtil.formatDateTime(new Date())
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.MORNINGTIME,
                                                                        "07:58"),
                                                        "",
                                                        "morninghello.wav",
                                                        1, 1, 7, 0,
                                                        0, 1, 0,
                                                        "", 0, 0,
                                                        "", "");
                                    } else {
                                        App.getDBcApplication()
                                                .insertEveryClockData(
                                                        -1,
                                                        today
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.MORNINGTIME,
                                                                        "07:58"),
                                                        "早上问候",
                                                        0,
                                                        today
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.MORNINGTIME,
                                                                        "07:58"),
                                                        "",
                                                        "morninghello.wav",
                                                        1, 1, 7, 0,
                                                        0, 1, 0,
                                                        "", 0, 0,
                                                        "", "");
                                    }
                                }
                            } else {
                                App.getDBcApplication()
                                        .deleteEveryClock(-1);
                            }
                            if ("0".equals(sharedPrefUtil
                                    .getString(getApplicationContext(),
                                            ShareFile.USERFILE,
                                            ShareFile.NIGHTSTATE,
                                            "0"))) {

                                int count = App.getDBcApplication()
                                        .CheckClockIDData(-2);
                                if (count == 0) {
                                    if (DateUtil
                                            .parseDateTimeHm(
                                                    sharedPrefUtil
                                                            .getString(
                                                                    getApplicationContext(),
                                                                    ShareFile.USERFILE,
                                                                    ShareFile.NIGHTTIME,
                                                                    "20:58"))
                                            .before(DateUtil
                                                    .parseDateTimeHm(DateUtil
                                                            .formatDateTimeHm(new Date())))) {
                                        App.getDBcApplication()
                                                .insertEveryClockData(
                                                        -2,
                                                        tomorrow
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.NIGHTTIME,
                                                                        "20:58"),
                                                        "下午问候",
                                                        0,
                                                        tomorrow
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.NIGHTTIME,
                                                                        "20:58"),
                                                        "",
                                                        "nighthello.ogg",
                                                        1, 1, 7, 0,
                                                        0, 1, 0,
                                                        "", 0, 0,
                                                        "", "");
                                    } else {
                                        App.getDBcApplication()
                                                .insertEveryClockData(
                                                        -2,
                                                        today
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.NIGHTTIME,
                                                                        "20:58"),
                                                        "下午问候",
                                                        0,
                                                        today
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.NIGHTTIME,
                                                                        "20:58"),
                                                        "",
                                                        "nighthello.ogg",
                                                        1, 1, 7, 0,
                                                        0, 1, 0,
                                                        "", 0, 0,
                                                        "", "");
                                    }
                                } else {
                                    App.getDBcApplication()
                                            .deleteEveryClock(-2);
                                    if (DateUtil
                                            .parseDateTimeHm(
                                                    sharedPrefUtil
                                                            .getString(
                                                                    getApplicationContext(),
                                                                    ShareFile.USERFILE,
                                                                    ShareFile.NIGHTTIME,
                                                                    "20:58"))
                                            .before(DateUtil
                                                    .parseDateTimeHm(DateUtil
                                                            .formatDateTimeHm(new Date())))) {
                                        App.getDBcApplication()
                                                .insertEveryClockData(
                                                        -2,
                                                        tomorrow
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.NIGHTTIME,
                                                                        "20:58"),
                                                        "下午问候",
                                                        0,
                                                        tomorrow
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.NIGHTTIME,
                                                                        "20:58"),
                                                        "",
                                                        "nighthello.ogg",
                                                        1, 1, 7, 0,
                                                        0, 1, 0,
                                                        "", 0, 0,
                                                        "", "");
                                    } else {
                                        App.getDBcApplication()
                                                .insertEveryClockData(
                                                        -2,
                                                        today
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.NIGHTTIME,
                                                                        "20:58"),
                                                        "下午问候",
                                                        0,
                                                        today
                                                                + " "
                                                                + sharedPrefUtil
                                                                .getString(
                                                                        getApplicationContext(),
                                                                        ShareFile.USERFILE,
                                                                        ShareFile.NIGHTTIME,
                                                                        "20:58"),
                                                        "",
                                                        "nighthello.ogg",
                                                        1, 1, 7, 0,
                                                        0, 1, 0,
                                                        "", 0, 0,
                                                        "", "");
                                    }
                                }
                            } else {
                                App.getDBcApplication().deleteEveryClock(-2);
                            }

                        }
                        downPersonStatus(false);
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                        downPersonStatus(true);
                    }
                }else {
                    downPersonStatus(true);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                downPersonStatus(true);
            }
        });
        request.setTag("down");
        request.setRetryPolicy(new DefaultRetryPolicy(10000, 1, 1.0f));
        App.getHttpQueues().add(request);
    }


    private void downPersonStatus(boolean isFailed){
        ServerDataDownLocalReceiver = new Intent("ServerDataDownLocalReceiver");
        ServerDataDownLocalReceiver.putExtra("downPerson", isFailed ? "downPerson_false" : "downPerson");
        sendBroadcast(ServerDataDownLocalReceiver);
    }
}
